package com.miracle.basemvp.base.state;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.miracle.basemvp.R;
import com.miracle.basemvp.base.BaseResponse;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

/**
 * StateController.java
 * (c)2018 AIR Times Inc. All rights reserved.
 * 网络状态控制
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/9 13:01
 * @see com.miracle.basemvp.base.state
 */
public class StateController {
    public Context mContext;
    private static final String MSG_NODATA = "暂无数据";
    private static final String MSG_ERROR = "数据异常";
    private static final int RES_NODATA = R.drawable.icon_no_data_2;
    private static final int RES_ERROR = R.drawable.icon_no_signal_2;

    public final LinearLayout stateLayout;
    public final TextView stateTv;
    public final ImageView stateIv;
    public final SmartRefreshLayout smartRefreshLayout;
    public final FrameLayout loadingLayout;
    public final String noDataMsg;
    public final String errorMsg;
    public final int noDataRes;
    public final int errorRes;

    private StateController(Builder builder) {

        if (builder.mActivity != null) {
            mContext = builder.mActivity;
        } else {
            mContext = builder.mFragment.getActivity();
        }
        stateLayout = builder.stateLayout;
        stateTv = builder.stateTv;
        stateIv = builder.stateIv;
        noDataMsg = builder.noDataMsg;
        errorMsg = builder.errorMsg;
        noDataRes = builder.noDataRes;
        errorRes = builder.errorRes;
        smartRefreshLayout = builder.smartRefreshLayout;
        loadingLayout = builder.loadingLayout;
    }

    public void onStart() {
        setLoadingState(true);
    }

    /**
     * 无数据状态
     */
    public void onNoDataState() {
        Log.d("WJQ", "onNoDataState");
        setLoadingState(false);
        closeSmartRefreshLayout();
        setStateLayoutVisiable(View.VISIBLE);
        setStateText(true);
        setStateRes(true);
    }

    public void onErrorState(String errorMsg) {
        Log.d("WJQ", "onErrorState: " + errorMsg);
        setLoadingState(false);
        closeSmartRefreshLayout();
        setStateLayoutVisiable(View.VISIBLE);
        setStateText(false);
        setStateRes(false);
    }

    public void onSuccessState() {
        closeSmartRefreshLayout();
        setStateLayoutVisiable(View.GONE);
        setLoadingState(false);
    }

    /**
     * 处理列表数据
     * @param mPageView
     * @param response
     */
    public void onSuccessState(IPageView mPageView, BaseResponse response, List list) {
        if (response.getContent() != null && list != null && list.size() > 0) {
            if (list.size() < mPageView.getPageSize()) {
                onNoLoadMoreState();
            } else {
                onSuccessState();
            }
            if (mPageView.isLoadMore()) {
                mPageView.loadMoreSucc(list);
            } else {
                mPageView.getListSucc(list);
            }
        } else {
            if (mPageView.isLoadMore()) {
                onNoLoadMoreState();
            } else {
                onNoDataState();
            }
        }
    }

    /**
     * 没有更多数据
     */
    public void onNoLoadMoreState() {
        onSuccessState();
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishLoadmoreWithNoMoreData();
        }
    }

    /**
     * 关闭刷新布局
     */
    private void closeSmartRefreshLayout() {
        if (smartRefreshLayout != null) {
            //关闭刷新
            smartRefreshLayout.finishRefresh();
            //关闭加载更多
            smartRefreshLayout.finishLoadmore();
        }
    }

    /**
     * 设置状态布局可见性
     * @param visiable {@link }
     */
    private void setStateLayoutVisiable(int visiable) {
        if (stateLayout != null) {
            stateLayout.setVisibility(visiable);
        }
    }

    /**
     * 设置刷新状态
     * @param isLoading
     */
    private void setLoadingState(boolean isLoading) {
        if (loadingLayout == null) {
            return;
        }

        if (isLoading) {
            loadingLayout.setVisibility(View.VISIBLE);
        } else {
            loadingLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 设置状态图片
     * @param isNodaState 是无数据状态
     */
    private void setStateRes(boolean isNodaState) {
        if (isNodaState) {
            if (stateIv != null) {
                stateIv.setImageResource((noDataRes != 0) ? noDataRes : RES_NODATA);
            }
        } else {
            if (stateIv != null) {
                stateIv.setImageResource((errorRes != 0) ? errorRes : RES_ERROR);
            }
        }
    }

    /**
     * 设置状态文字
     * @param isNodaState 是无数据状态
     */
    private void setStateText(boolean isNodaState) {
        if (isNodaState) {
            if (stateTv != null) {
                stateTv.setText((TextUtils.isEmpty(noDataMsg) ? MSG_NODATA : noDataMsg));
            }
        } else {
            if (stateTv != null) {
                stateTv.setText((TextUtils.isEmpty(noDataMsg) ? MSG_ERROR : errorMsg));
            }
        }
    }

    /**
     * 构造器
     */
    public static class Builder {
        private Activity mActivity;
        private Fragment mFragment;

        private LinearLayout stateLayout;
        private TextView stateTv;
        private ImageView stateIv;
        private String noDataMsg;
        private String errorMsg;
        private int noDataRes;
        private int errorRes;
        private SmartRefreshLayout smartRefreshLayout;
        private FrameLayout loadingLayout;

        public Builder initBuilder(Activity activity) {
            mActivity = activity;
            return this;
        }

        public Builder initBuilder(Fragment fragment) {
            mFragment = fragment;

            return this;
        }

        public Builder setLayoutState(int id) {
//            findView(mActivity, mFragment, id, this.stateLayout);
            if (mActivity != null) {
                this.stateLayout = mActivity.findViewById(id);
            } else if (mFragment != null) {
                this.stateLayout = mFragment.getView().findViewById(id);
            }
            return this;
        }

        public Builder setStateTextView(int id) {
            if (mActivity != null) {
                this.stateTv = mActivity.findViewById(id);
            } else if (mFragment != null) {
                this.stateTv = mFragment.getView().findViewById(id);
            }
//            findView(mActivity, mFragment, id, this.stateTv);
            return this;
        }

        public Builder setNoDataText(String text) {
            this.noDataMsg = text;
            return this;
        }

        public Builder setErrorText(String text) {
            this.errorMsg = text;
            return this;
        }

        public Builder setStateImageView(int id) {
            if (mActivity != null) {
                this.stateIv = mActivity.findViewById(id);
            } else if (mFragment != null) {
                this.stateIv = mFragment.getView().findViewById(id);
            }
//            findView(mActivity, mFragment, id, this.stateIv);
            return this;
        }

        public Builder setNodataRes(int id) {
            this.noDataRes = id;
            return this;
        }

        public Builder setErrorRes(int id) {
            this.errorRes = id;
            return this;
        }

        public Builder setSmartRefreshLayout(int id) {
            if (mActivity != null) {
                this.smartRefreshLayout = mActivity.findViewById(id);
            } else if (mFragment != null) {
                this.smartRefreshLayout = mFragment.getView().findViewById(id);
            }
//            findView(mActivity, mFragment, id, this.smartRefreshLayout);
            return this;
        }

        public Builder setLoadingLayout(int id) {
            if (mActivity != null) {
                this.loadingLayout = mActivity.findViewById(id);
            } else if (mFragment != null) {
                this.loadingLayout = mFragment.getView().findViewById(id);
            }
//            findView(mActivity, mFragment, id, this.smartRefreshLayout);
            return this;
        }

        public StateController build() {
            return new StateController(this);
        }
    }
}
